package whu.cachhe.makeorder.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import whu.cachhe.makeorder.domain.User;
import whu.cachhe.makeorder.net.HttpRequest;
import whu.cachhe.makeorder.net.URLConstant;

/**
 * 这个类的任何改动不反应在 主系统里
 * Created by CocoAdapter on 2018/4/4.
 */

public class UserMappingActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//        String userId = getIntent().getStringExtra("userId");
//        int userType = getIntent().getIntExtra("userType", 0);

        // TODO TEST
        String userId = "16000003";
        int userType = 0;

        getMappingUID(userId, userType);
    }

    private void getMappingUID(final String usrId, final int userType){
        try {
            JSONObject fparam = new JSONObject();
            fparam.put("usrId", usrId);

            HttpRequest.post(URLConstant.HOST, "getMappingUid", fparam, new HttpRequest.HttpRequestCallback() {
                @Override
                public void onSuccess(JSONObject response) {
                    if (response.optInt("success") == 1){
                        JSONObject jR = response.optJSONObject("result");
                        String uid = jR.optString("uid");

                        User user = new User(usrId, uid, userType);

                        Intent intent = new Intent(UserMappingActivity.this, MainActivity.class);
                        intent.putExtra("user", user);
                        startActivity(intent);
                    } else {
                        Toast.makeText(UserMappingActivity.this, response.optString("result"), Toast.LENGTH_LONG).show();
                    }

                    finish();
                }

                @Override
                public void onFailure() {
                    Toast.makeText(UserMappingActivity.this, "网络异常", Toast.LENGTH_LONG).show();
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
